package cn.huapu.power.energy.controller;

import cn.huapu.power.energy.bean.SysClient;
import cn.huapu.power.energy.bean.SysUser;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.bean.vo.ClientpowerhourstDayVo;
import cn.huapu.power.energy.bean.vo.ClientpowerstVo;
import cn.huapu.power.energy.bean.vo.ClntPowerVo;
import cn.huapu.power.energy.bean.vo.SysClientVo;
import cn.huapu.power.energy.service.*;
import cn.huapu.power.energy.util.ObjectHelper;
import cn.huapu.power.energy.vo.Ret;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author jiangzhengxiong
 * @title: EnergyWholeController
 * @projectName energy
 * @description: 电能整体情况接口
 * @date 2019-5-2710:40
 */
@RestController
@RequestMapping("/whole")
public class EnergyWholeController {

    private Log log = LogFactory.getLog(EnergyWholeController.class);

    @Autowired
    private ClientpowerstService clienService;
    @Autowired
    private ClntpwrsourcetService clntService;
    @Autowired
    private ClientpowerhourstService clihouService;
    @Autowired
    private SysClientService sysClientService;
    @Autowired
    private SysUserService sysUserService;

    public final static Integer ROLEID = 2;//客户角色

    /**
    * @Author jzx
    * @Description  首页查询接口
    * @Date 11:02 2019-5-27
    * @Param [httpSession]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/queryCompany",method = RequestMethod.POST)
    public Ret queryCompany(@RequestBody(required = false) DateParameterBo bo,HttpSession httpSession){
        SysUser user = (SysUser)httpSession.getAttribute("user");
        if(bo==null){
            bo = new DateParameterBo();
        }
        if(bo.getName()==null){
            bo.setName("");
        }

        //先查询出系统管理员所有用户的Entityid
//        List<String> entityids = pcmUsepowerunitService.queryListRoleEntityids();
//        boolean contains = entityids.contains(user.getEntityid());
//        List<PcmUsepowerunitVo> pcmunts;
//        PcmUsepowerunitVo  pcmunt = null;
//        if(contains){
//            //查询所有单位id与名称
//            pcmunts = pcmUsepowerunitService.queryTopAllentityids();
//        }else{
//            //查询该用户下的单位id与名称
//            pcmunts = pcmUsepowerunitService.getByid(user.getEntityid());
//            //查询该用户下的所有单位id与名称
//            pcmunt = pcmUsepowerunitService.getByids(user.getEntityid());
//        }

        //根据当前用户查询角色为1的就是系统用户
        List<Integer> roles= sysUserService.getRoleId(user.getPkid());
        SysClientVo sysClientVo = new SysClientVo();
        Ret r = new Ret();
        if(roles == null || roles.isEmpty() || roles.contains(ROLEID)){
            //查询该用户下的所有单位id与名称
            sysClientVo = sysClientService.getByids(bo.getName(),user.getClientid());
            r.OK(sysClientVo);
            return r;
        }
        //查询所有单位id
        List<SysClientVo> list = sysClientService.getAllIds(bo.getName());
        sysClientVo = new SysClientVo();
        sysClientVo.setId(user.getClientid());
        sysClientVo.setName(sysClientService.get(user.getClientid()).getName());
        sysClientVo.setNodes(list);
        r.OK(sysClientVo);
        return r;
    }
    
    /**
    * @Author jzx 
    * @Description  本周环比上周
    * @Date 11:14 2019-5-27
    * @Param [year, month] 
    * @return cn.huapu.power.energy.vo.Ret 
    **/
    @RequestMapping(value = "/count",method = RequestMethod.POST)
    public Ret count(@RequestBody DateParameterBo da,HttpSession session) throws ParseException {
    	LocalDate date = null;
    	if (da.getId() == null){
    		SysUser user = (SysUser) session.getAttribute("user");
    		da.setId(user.getClientid());
        }
    	if(da.getDateOne() != null && !da.getDateOne().isEmpty()) {//格式不标准
    		String[] strs = da.getDateOne().split("-");
    		if(strs.length == 3) {
    			date = LocalDate.of(Integer.valueOf(strs[0]), Integer.valueOf(strs[1]), Integer.valueOf(strs[2]));
    		}
    	}
    	
    	date = date== null ? LocalDate.now():date;
       
        //上周一的时间）      
        LocalDate datePreMon = date.plusWeeks(-1).with(DayOfWeek.MONDAY);
        //上周日的时间
        LocalDate datePreSun = datePreMon.plusDays(6);
        //本周一的时间
        LocalDate dateMon = date.with(DayOfWeek.MONDAY);
        //当前时间da.getDateOne()

        List<Double> weekClies = clienService.getElecWeek(da,datePreMon.toString(),datePreSun.toString());
        List<Double> weekClies1 = clienService.getElecWeek(da,dateMon.toString(),date.toString());
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("weekBZClies",weekClies);
        map.put("weekSZClies",weekClies1);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }

    /**
    * @Author jzx
    * @Description //月购买电量消费
    * @Date 12:34 2019-6-17
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/monthBuyElec",method = RequestMethod.POST)
    public Ret monthBuyElec(@RequestBody DateParameterBo da){
        List<ClientpowerstVo> clies = clienService.getMonthBuyElec(da);
        Ret r = new Ret();
        r.OK(clies);
        return r;
    }

    /**
    * @Author jzx
    * @Description //显示每天的总电量
    * @Date 14:16 2019-6-17
    * @Param [da]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/dayBuyElec",method = RequestMethod.POST)
    public Ret dayBuyElec(@RequestBody DateParameterBo da){
        List<ClientpowerstVo> cliesDay =  clienService.getDayBuyElec(da);
        Ret r = new Ret();
        r.OK(cliesDay);
        return r;
    }


    /**
    * @Author jzx
    * @Description 电量整体同比图
    * @Date 8:44 2019-5-28
    * @Param [session]
    * @return cn.huapu.power.energy.vo.Ret
    **/
    @RequestMapping(value = "/electTb",method = RequestMethod.POST)
    public Ret electTb(@RequestBody DateParameterBo da,HttpSession session){
        String[] split = da.getDateOne().split("-");
        String year = split[0];
        String month = split[1].replace("0","");
        String day = split[2].replace("0","");
        //同比时间之一
        ClientpowerhourstDayVo clienvo = clihouService.getDayElects(year,month,day,da.getId());

        String[] split1 = da.getDateTwo().split("-");
        String year1 = split1[0];
        String month1 = split1[1].replace("0","");
        String day1 = split1[2].replace("0","");
        //同比时间之一
        ClientpowerhourstDayVo clienvo1 = clihouService.getDayElects(year1,month1,day1,da.getId());
        Map<String,Object> map = new HashMap<>(5);
        map.put("dateOneElecs",clienvo);
        map.put("dateTwoElecs",clienvo1);
        Ret r = new Ret();
        r.OK(map);
        return r;
    }
    
    /**
    * @Author jzx 
    * @Description  总电量构成图
    * @Date 15:45 2019-5-27
    * @Param [state] 
    * @return cn.huapu.power.energy.vo.Ret 
    **/
    @RequestMapping(value = "/constitute",method = RequestMethod.POST)
    public Ret constitute(@RequestBody DateParameterBo da,HttpSession session){
        String[] split = da.getDateOne().split("-");
        String year = split[0];
        String month = split[1].replace("0","");

        String[] split1 = da.getDateTwo().split("-");
        String year1 = split[0];
        String month1 = split[1].replace("0","");
        ClntPowerVo clntPowerVo = clntService.getTotle(year,month,year1,month1,da.getId());
        Ret r = new Ret();
        r.OK(clntPowerVo);
        return r;
    }





}
